home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 17 controls / databindingdemo / simplebindingform.vb < prev    next >
Text File  |  2002-03-16  |  10KB  |  250 lines

  1. Public Class SimpleBindingForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents Label1 As System.Windows.Forms.Label
  26.     Friend WithEvents txtFirstName As System.Windows.Forms.TextBox
  27.     Friend WithEvents lblRecord As System.Windows.Forms.Label
  28.     Friend WithEvents btnPrevious As System.Windows.Forms.Button
  29.     Friend WithEvents btnNext As System.Windows.Forms.Button
  30.     Friend WithEvents btnLast As System.Windows.Forms.Button
  31.     Friend WithEvents btnFirst As System.Windows.Forms.Button
  32.     Friend WithEvents Label2 As System.Windows.Forms.Label
  33.     Friend WithEvents txtLastName As System.Windows.Forms.TextBox
  34.     Friend WithEvents Label3 As System.Windows.Forms.Label
  35.     Friend WithEvents txtBirthDate As System.Windows.Forms.TextBox
  36.     Friend WithEvents chkPartTime As System.Windows.Forms.CheckBox
  37.     
  38.     'Required by the Windows Form Designer
  39.     Private components As System.ComponentModel.Container
  40.  
  41.     'NOTE: The following procedure is required by the Windows Form Designer
  42.     'It can be modified using the Windows Form Designer.  
  43.     'Do not modify it using the code editor.
  44.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  45.         Me.txtBirthDate = New System.Windows.Forms.TextBox()
  46.         Me.btnLast = New System.Windows.Forms.Button()
  47.         Me.txtFirstName = New System.Windows.Forms.TextBox()
  48.         Me.Label1 = New System.Windows.Forms.Label()
  49.         Me.btnPrevious = New System.Windows.Forms.Button()
  50.         Me.Label3 = New System.Windows.Forms.Label()
  51.         Me.btnFirst = New System.Windows.Forms.Button()
  52.         Me.btnNext = New System.Windows.Forms.Button()
  53.         Me.lblRecord = New System.Windows.Forms.Label()
  54.         Me.txtLastName = New System.Windows.Forms.TextBox()
  55.         Me.chkPartTime = New System.Windows.Forms.CheckBox()
  56.         Me.Label2 = New System.Windows.Forms.Label()
  57.         Me.SuspendLayout()
  58.         '
  59.         'txtBirthDate
  60.         '
  61.         Me.txtBirthDate.Location = New System.Drawing.Point(16, 112)
  62.         Me.txtBirthDate.Name = "txtBirthDate"
  63.         Me.txtBirthDate.Size = New System.Drawing.Size(192, 24)
  64.         Me.txtBirthDate.TabIndex = 5
  65.         Me.txtBirthDate.Text = ""
  66.         '
  67.         'btnLast
  68.         '
  69.         Me.btnLast.Location = New System.Drawing.Point(320, 168)
  70.         Me.btnLast.Name = "btnLast"
  71.         Me.btnLast.Size = New System.Drawing.Size(48, 32)
  72.         Me.btnLast.TabIndex = 11
  73.         Me.btnLast.Text = ">>"
  74.         '
  75.         'txtFirstName
  76.         '
  77.         Me.txtFirstName.Location = New System.Drawing.Point(16, 48)
  78.         Me.txtFirstName.Name = "txtFirstName"
  79.         Me.txtFirstName.Size = New System.Drawing.Size(192, 24)
  80.         Me.txtFirstName.TabIndex = 1
  81.         Me.txtFirstName.Text = ""
  82.         '
  83.         'Label1
  84.         '
  85.         Me.Label1.Location = New System.Drawing.Point(16, 24)
  86.         Me.Label1.Name = "Label1"
  87.         Me.Label1.Size = New System.Drawing.Size(168, 24)
  88.         Me.Label1.TabIndex = 0
  89.         Me.Label1.Text = "First Name"
  90.         '
  91.         'btnPrevious
  92.         '
  93.         Me.btnPrevious.Location = New System.Drawing.Point(136, 168)
  94.         Me.btnPrevious.Name = "btnPrevious"
  95.         Me.btnPrevious.Size = New System.Drawing.Size(48, 32)
  96.         Me.btnPrevious.TabIndex = 8
  97.         Me.btnPrevious.Text = "<"
  98.         '
  99.         'Label3
  100.         '
  101.         Me.Label3.Location = New System.Drawing.Point(16, 88)
  102.         Me.Label3.Name = "Label3"
  103.         Me.Label3.Size = New System.Drawing.Size(168, 24)
  104.         Me.Label3.TabIndex = 4
  105.         Me.Label3.Text = "Birth Date"
  106.         '
  107.         'btnFirst
  108.         '
  109.         Me.btnFirst.Location = New System.Drawing.Point(88, 168)
  110.         Me.btnFirst.Name = "btnFirst"
  111.         Me.btnFirst.Size = New System.Drawing.Size(48, 32)
  112.         Me.btnFirst.TabIndex = 7
  113.         Me.btnFirst.Text = "<<"
  114.         '
  115.         'btnNext
  116.         '
  117.         Me.btnNext.Location = New System.Drawing.Point(272, 168)
  118.         Me.btnNext.Name = "btnNext"
  119.         Me.btnNext.Size = New System.Drawing.Size(48, 32)
  120.         Me.btnNext.TabIndex = 10
  121.         Me.btnNext.Text = ">"
  122.         '
  123.         'lblRecord
  124.         '
  125.         Me.lblRecord.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  126.         Me.lblRecord.Location = New System.Drawing.Point(184, 168)
  127.         Me.lblRecord.Name = "lblRecord"
  128.         Me.lblRecord.Size = New System.Drawing.Size(88, 32)
  129.         Me.lblRecord.TabIndex = 9
  130.         Me.lblRecord.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
  131.         '
  132.         'txtLastName
  133.         '
  134.         Me.txtLastName.Location = New System.Drawing.Point(232, 48)
  135.         Me.txtLastName.Name = "txtLastName"
  136.         Me.txtLastName.Size = New System.Drawing.Size(192, 24)
  137.         Me.txtLastName.TabIndex = 3
  138.         Me.txtLastName.Text = ""
  139.         '
  140.         'chkPartTime
  141.         '
  142.         Me.chkPartTime.Location = New System.Drawing.Point(232, 112)
  143.         Me.chkPartTime.Name = "chkPartTime"
  144.         Me.chkPartTime.Size = New System.Drawing.Size(176, 16)
  145.         Me.chkPartTime.TabIndex = 6
  146.         Me.chkPartTime.Text = "PartTime Employee"
  147.         '
  148.         'Label2
  149.         '
  150.         Me.Label2.Location = New System.Drawing.Point(232, 24)
  151.         Me.Label2.Name = "Label2"
  152.         Me.Label2.Size = New System.Drawing.Size(168, 24)
  153.         Me.Label2.TabIndex = 2
  154.         Me.Label2.Text = "Last Name"
  155.         '
  156.         'SimpleBindingForm
  157.         '
  158.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  159.         Me.ClientSize = New System.Drawing.Size(456, 213)
  160.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkPartTime, Me.Label3, Me.txtBirthDate, Me.Label2, Me.txtLastName, Me.lblRecord, Me.btnLast, Me.btnNext, Me.btnPrevious, Me.btnFirst, Me.txtFirstName, Me.Label1})
  161.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  162.         Me.Name = "SimpleBindingForm"
  163.         Me.Text = "Simple Data Binding"
  164.         Me.ResumeLayout(False)
  165.  
  166.     End Sub
  167.  
  168. #End Region
  169.  
  170.     ' an array of object that works as a data source
  171.  
  172.     Dim employees() As Employee = {New Employee("Joe", "Doe", #1/3/1960#), _
  173.         New Employee("Robert", "Smith", #11/23/1962#), _
  174.         New Employee("Ann", "Ross", #2/5/1965#, True)}
  175.  
  176.     Dim WithEvents bmb As BindingManagerBase
  177.  
  178.     ' initialize data binding when the form loads
  179.  
  180.     Private Sub SimpleBindingForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  181.         InitializeBinding()
  182.     End Sub
  183.  
  184.     Sub InitializeBinding()
  185.         ' Create the Binding object for each bound field.
  186.         txtFirstName.DataBindings.Add("Text", employees, "FirstName")
  187.         txtLastName.DataBindings.Add("Text", employees, "LastName")
  188.  
  189.         ' Get a reference to this binding and bind it to two events.
  190.         Dim bnd As Binding = txtBirthDate.DataBindings.Add("Text", employees, "BirthDate")
  191.         AddHandler bnd.Format, AddressOf DataFormat
  192.         AddHandler bnd.Parse, AddressOf DataParse
  193.  
  194.         chkPartTime.DataBindings.Add("Checked", employees, "PartTimeEmployee")
  195.  
  196.         ' Save a reference to the BindingManagerBase object.
  197.         bmb = Me.BindingContext(employees)
  198.         ' Force a refresh of buttons
  199.         bmb.Position = bmb.Count
  200.         bmb.Position = 0
  201.     End Sub
  202.  
  203.     ' navigational buttons
  204.  
  205.     Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
  206.         bmb.Position = 0
  207.     End Sub
  208.  
  209.     Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
  210.         bmb.Position -= 1
  211.     End Sub
  212.  
  213.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  214.         bmb.Position += 1
  215.     End Sub
  216.  
  217.     Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
  218.         bmb.Position = bmb.Count - 1
  219.     End Sub
  220.  
  221.     ' Display current position in label.
  222.     Private Sub bmb_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bmb.PositionChanged
  223.         lblRecord.Text = String.Format("{0} of {1}", bmb.Position + 1, bmb.Count)
  224.         ' Enable/disable buttons.
  225.         btnFirst.Enabled = (bmb.Position > 0)
  226.         btnPrevious.Enabled = (bmb.Position > 0)
  227.         btnNext.Enabled = (bmb.Position < bmb.Count - 1)
  228.         btnLast.Enabled = (bmb.Position < bmb.Count - 1)
  229.     End Sub
  230.  
  231.     ' an example of how you can format values when they're taken
  232.     ' from the data source
  233.  
  234.     Private Sub DataFormat(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs)
  235.         If sender Is txtBirthDate Then
  236.             e.Value = String.Format("{0:dd-MM-yyyy}", e.Value)
  237.         End If
  238.     End Sub
  239.  
  240.     ' an example of how you can parse data before it's stored back
  241.     ' in the data source
  242.  
  243.     Private Sub DataParse(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs)
  244.         If sender Is txtBirthDate Then
  245.             e.Value = CDate(e.Value)
  246.         End If
  247.     End Sub
  248. End Class
  249.  
  250.